﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class="layui-form-label">权限编码</label>
                    <div class="layui-input-block">
                        <input type="text" id="permissionCoding" placeholder="请输入" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">权限名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="permissionName" placeholder="请输入" class="layui-input">
                    </div>

                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">权限类型</label>
                    <div class="layui-input-block">
                        <select name="interest" lay-filter="testSelect1" id="permissionTypeId">
                            <option value="0" selected>请选择</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select id="permissionState" lay-filter="testSelect2">
                            <option value="0">请选择</option>
                            <option value="1">有效</option>
                            <option value="2">无效</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="demo-table-search">查询</button>
                    </div>
                </div>
                <button type="button" class="layui-btn layui-bg-red" lay-on="test-iframe-curl" onclick="AddPermission()">+新增</button>
            </div>
        </div>

        <table class="layui-table">
            <thead>
                <tr>
                    <td>序号</td>
                    <td>权限编码</td>
                    <td>权限名称</td>
                    <td>创建人</td>
                    <td>创建时间</td>
                    <td>状态</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
        <div id="demo-laypage-all"></div>
    </form>
    <!-- 弹出层 -->
    <script>
        layui.use(function () {
            var $ = layui.$;
            var layer = layui.layer;
            var util = layui.util;
            var form = layui.form;
            // 事件
            util.on('lay-on', {
                'test-page-custom': function () {
                    layer.open({
                        type: 2,
                        title: '权限编辑',
                        shadeClose: true,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['900px', '600px'],
                        content: '/Permission/UpdatePermission',
                    });
                },
            });
        });
    </script>
    <script src="//unpkg.com/layui@2.9.18/dist/layui.js"></script>
    <script>
  
        layui.use(['form', 'laydate', 'util'], function () {
            var form = layui.form
            var laypage = layui.laypage;
            var $ = layui.$;
            var layer = layui.layer;
            var util = layui.util;
            var pageIndex = 1;
            var pageSize = 5;
            $(function () {
                GetPer();
                ShowPermission(pageIndex, pageSize,0,0);
            })

            
            //权限分类列表
            function GetPer() {
                $.ajax({
                    url: '@ViewBag.RBACWrite' + "/api/Permissions/GetPermissionTypes",
                    data: JSON.stringify({}),
                    type: "post",
                    dataType: "json",
                    contentType: "application/json",
                    success: res => {
                        $.each(res.data, function () {
                            $("#permissionTypeId").append(`<option value="${this.permissionTypeId}">${this.permissionTypeName}</option>`);
                        });
                        form.render('select'); // 重新渲染下拉框
                    },
                });
            }
            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                ShowPermission(pageIndex, pageSize, $("#permissionTypeId").val(), $("#permissionState").val()); 
                
                return false; // 阻止默认 form 跳转
            });

            let totalcou = 1;
            let pagecou = 5;
            //权限列表
            function ShowPermission(val,sass,typeid,state) {
                let data = {
                    permissionCoding: $("#permissionCoding").val(),
                    permissionName: $("#permissionName").val(),
                    permissionTypeId: typeid,
                    permissionState: state,
                    pageIndex: val,
                    pageSize: sass
                }
                console.log(data)
                $.ajax({
                    url: '@ViewBag.RBACWrite' + "/api/Permissions/QueryCondition",
                    data: JSON.stringify(data),
                    type: "post",
                    dataType: "json",
                    contentType: "application/json",
                    success: res => {
                        var tbody = $("tbody");
                        tbody.empty(); // 清空tbody内容
                        if (res.data != null) {
                            totalcou = res.data.totalCount;
                            pagecou = res.data.pageCount;
                            $.each(res.data.list, function () {
                                tbody.append(`
                                    <tr>
                                        <td>${this.permissionId}</td>
                                        <td>${this.permissionCoding}</td>
                                        <td>${this.permissionName}</td>
                                        <td>${this.createdOwner}</td>
                                        <td>${this.createdTime}</td>
                                        <td>${this.permissionState === 1 ? '有效' : '无效'}</td>
                                        <td>
                                        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-on="test-page-custom"  onclick="UpdatePermission(${this.permissionId})">编辑</button>
                                        <button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="DeletePermission(${this.permissionId})">删除</button>
                                    </td>
                                    </tr>
                                     `);
                            }); 
                        }
                        
                        laypage.render({
                            elem: 'demo-laypage-all', // 使用正确的ID
                            curr: pageIndex, // 当前页
                            count: totalcou, // 数据总数
                            limit: pageSize, // 每页显示的条数
                            limits: [5, 10, 15], // 每页可以显示多少条
                            layout: ['count', 'prev', 'page', 'first', 'last', 'next', 'limit', 'skip'], // 自定义布局
                            jump: function (obj, first) {
                                if (!first) {
                                    pageIndex = obj.curr; // 更新当前页
                                    pageSize = obj.limit; // 更新每页显示的条数（如果需要的话）
                                    console.log(pageSize, pageIndex, "123123$$$");
                                    ShowPermission(pageIndex, pageSize);
                                    form.render('table'); // 重新渲染下拉框
                                }
                                // 重新加载数据
                            }
                        });
                        console.log(996, pageSize, pageIndex, res);
                    }
                });
            }
        });

        //删除权限
        function DeletePermission(permissionId) {
            if (confirm("确认要删除这个权限嘛？")) {
                var data = {
                    permissionId: permissionId
                }
                $.ajax({
                    url: '@ViewBag.RBACWrite' + "/api/Permissions/DeletePermission",
                    data: JSON.stringify(data),
                    type: "DELETE",
                    dataType: "json",
                    contentType: "application/json",
                    success: res => {
                        if (res.code == 0) {
                            layer.msg('删除成功');
                            location.reload();
                        }
                        else {
                            layer.msg('删除失败');
                        }
                    }
                })
            } else {
                // 用户点击了取消按钮，不执行删除操作
                layer.msg('已取消删除');
            }
        }
    </script>
</body>
</html>


<script>

    //权限添加
    function AddPermission() {
        location.href = "/Permission/AddPermission"
    }

    //修改
    function UpdatePermission(permissionId) {
        console.log(permissionId);
        localStorage.setItem("permissionId", permissionId)
        location.href = '/Permission/UpdatePermission'
    }

</script>
